package FollowTest;

import java.io.*;
import java.net.Socket;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",10000);
        //连接成功来个提示信息
        System.out.println("服务器已经连接成功");

        //先把流获取出来
        BufferedWriter bw = new BufferedWriter
                (new OutputStreamWriter(socket.getOutputStream()));

        while (true) {
            System.out.println("==========欢迎来到网络聊天室v2.0==========");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.nextLine();
            switch (choose)
            {
                case "1" -> login(socket);
                case "2" -> register(socket);
                default -> System.out.println("没有这个选项");
            }
        }

    }

    public static void register(Socket socket) throws IOException
    {
        //获取输出流
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));

        //输入用户名，密码，再次密码
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = sc.nextLine();
        System.out.println("请输入密码：（6-16位，只能包含小写字母和数字）");
        String password = sc.nextLine();
        System.out.println("请再次输入密码：");
        String againPassword = sc.nextLine();

        StringBuilder sb = new StringBuilder();
        sb.append("userName=").append(userName).
                append("&password=").append(password).
                append("&againPassword=").append(againPassword);
        //往服务器写出
        //先写出操作
        write2Server(bw,"register");
        //再写出用户信息
        write2Server(bw,sb.toString());

        //获取输入流
        BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        String message = br.readLine();

        if("1".equals(message))
        {
            System.out.println("用户名已存在！");
        }
        else if("2".equals(message))
        {
            System.out.println("密码必须是6-16位，且只包含数字和小写字母");
        }
        else if("3".equals(message))
        {
            System.out.println("密码两次输入不一致");
        }
        else if("4".equals(message))
        {
            System.out.println("注册成功");
            //返回主页面
        }
    }

    public static void login(Socket socket) throws IOException {
        //获取输出流
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));
        //输入用户名和密码
        Scanner sc = new Scanner(System.in);
        //提示并输入用户名和密码
        System.out.println("请输入用户名：");
        String userName = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        StringBuilder sb = new StringBuilder();
        //userName=zhangsan&password=123
        sb.append("userName=").append(userName).append("&")
                .append("password=").append(password);

        //第一个写的是什么操作
        write2Server(bw, "login");
        //每一句话都要newline和flush
        //往服务器写出用户名和密码
        //第二次写的是用户名和密码的信息
        write2Server(bw, sb.toString());
        //数据这样才到服务器

        //接收数据
        //获取输入流
        BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        String message = br.readLine();

        //状态码
        //1 登录成功
        //2 密码有误
        //3 用户名不存在

        if("1".equals(message))
        {
            //登录成功
            //死循环模拟聊天
            System.out.println("登录成功！");
            //开一条单独的线程，专门用来接收服务端发送过来的聊天记录
            new Thread(new ClientMyRunnable(socket)).start();
            //while (true){}
            // 聊天代码
            //往服务器写出消息
            talk2All(bw);
        }
        else if ("2".equals(message))
        {
            System.out.println("密码输入错误！");
        }
        else if("3".equals(message))
        {
            System.out.println("用户名不存在！");
        }
    }
    public static void talk2All(BufferedWriter bw) throws IOException
    {
        Scanner sc = new Scanner(System.in);
        while (true)
        {
            System.out.println("请输入您要说的话：");
            String str = sc.nextLine();
            //把聊天内容传给服务器
            write2Server(bw, str);
        }
    }

    private static void write2Server(BufferedWriter bw, String str) throws IOException {
        bw.write(str);
        bw.newLine();
        bw.flush();
    }
}


class ClientMyRunnable implements Runnable
{
    Socket socket;
    public ClientMyRunnable(Socket socket)
    {
        this.socket = socket;
    }

    @Override
    public void run() {
        //重复地接收信息
        while (true) {
            try {
                //接收服务器发送过来的聊天记录
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));

                String msg = br.readLine();

                System.out.println(msg);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


